# -*- coding: utf-8 -*-

class CustomerController < ApplicationController
  before_filter :employee_authorize
  before_filter :sys_before_log
  before_filter :employee_action_hx
  before_filter :privilege_filter,:except=>[:customerLock]
  
  #客户设置页面
  def customerList

  end

  #显示添加客户页面
  def newCustomer
    
  end

  #显示编辑客户页面
  def editCustomer
    
  end

  def getAllCustomers
    #是否专为表单准备？如果只是为销售订单而非编辑Customer本身准备的就不传过多的信息
    forCBB = params[:for_cbb]
    if forCBB != 'true'
      forCBB = 'false'
    end
    start = params[:start]
    limit = params[:limit]
    if params[:query].nil?
      queryStr = ""
    else
      queryStr = params[:query]
    end
    customers = Customer.name_like queryStr,forCBB
    if start.nil?
      customersNow = customers
    else
      customersNow = customers.paginate start,limit
    end

    if forCBB == 'true'
      jsonStr="{totalProperty:"+customers.length.to_s+",root:"+customersNow.to_json()+"}"    
    else
      jsonStr="{totalProperty:"+customers.length.to_s+",root:"+customersNow.to_json(:include=>[:adder,:editor,:customer_type])+"}"
    end
    logger.info jsonStr
    render :text=>jsonStr, :layout=>false
  end

  #添加客户页面
  def newCustomer

  end

  def create    
    begin
      customer = Customer.new()
      customer.company_id = session[:company_id]
      customer.number = params[:number]
      customer.customer_type_id = params[:customer_type_id]
      customer.name = params[:name]
      customer.phone = params[:phone]
      customer.fax = params[:fax]
      customer.address = params[:address]
      customer.linkman = params[:linkman]
      customer.email = params[:email]
      customer.bank = params[:bank]
      customer.bank_account = params[:bank_account]
      customer.description = params[:description]
      customer.adder_id = session[:employee_id]
      customer.save!
      info = "成功"
    rescue Exception => e
      info = e
      DbLog.info(session[:company_id], session[:department_id], session[:employee_id], params[:controller], params[:action], request.remote_ip ,e.to_s)
    end
    result = "{success:true,info:'#{info}'}"
    render :text=>result, :layout=>false
  end
  #编辑前的数据准备
  def edit
    customer = Customer.find(params[:id])
    rootStr = customer.to_json()
    jsonStr = "{root:["+rootStr+"]}"
    logger.info jsonStr
    render :text=>jsonStr, :layout=>false
  end

  #把编辑后的数据，更新到数据库中
  def update
    begin
      customer = Customer.find(params[:id])
      customer.number = params[:number]
      customer.customer_type_id = params[:customer_type_id]
      customer.name = params[:name]
      customer.phone = params[:phone]
      customer.fax = params[:fax]
      customer.address = params[:address]
      customer.linkman = params[:linkman]
      customer.email = params[:email]
      customer.bank = params[:bank]
      customer.bank_account = params[:bank_account]
      customer.description = params[:description]
      customer.editor_id = session[:employee_id]
      customer.save!
      info = "成功"
    rescue Exception => e
      info = e.to_s
      DbLog.info(session[:company_id], session[:department_id], session[:employee_id], params[:controller], params[:action], request.remote_ip ,e.to_s)
    end
    result = "{success:true,info:'#{info}'}"
    render :text=>result, :layout=>false
  end

  #锁定或解锁客户
  def customerLock
    customer = Customer.find(params[:id])
    if customer.is_locked == 1
      customer.is_locked = 0
    else
      customer.is_locked = 1
    end
    customer.save!
    info = "成功"
    result = "{success:true,info:'#{info}'}"
    render :text=>result, :layout=>false
  end

end
